﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using caedisi.Web.DataModel;
using System.ComponentModel;
using caedisi.Controller;
using caedisi.Views.Controls;
using caedisi.Models;
using caedisi.Common;
using caedisi.Helpers;
using System.Collections.ObjectModel;
using System.Text;

namespace caedisi.Views
{
    /// <summary>
    /// 
    /// </summary>
    public partial class InverterParameters : ChildWindow, INotifyPropertyChanged
    {       
        int numberOfRulesCreated = 0;

        /// <summary>
        /// Initializes a new instance of the <see cref="InverterParameters"/> class.
        /// </summary>
        public InverterParameters()
        {
            InitializeComponent();
        }


        /// <summary>
        /// Initializes a new instance of the <see cref="InverterParameters"/> class.
        /// </summary>
        /// <param name="inverterExecution">The inverter execution.</param>
        /// <param name="cellularAutomata">The cellular automata.</param>
        /// <param name="editMode">The edit mode.</param>
        public InverterParameters(InverterExecution inverterExecution, CellularAutomata cellularAutomata, EnumInverterParametersEditMode editMode)
        {
            this.CurrentInverterExecution = inverterExecution;
            this.CurrentCellularAutomata = cellularAutomata;
            this.EditMode = editMode;
            InitializeComponent();            
        }


        private InverterExecution _CurrentInverterExecution;
        /// <summary>
        /// Gets or sets the current simulation.
        /// </summary>
        /// <value>
        /// The current simulation.
        /// </value>
        public InverterExecution CurrentInverterExecution
        {
            get
            {
                return _CurrentInverterExecution;
            }
            set
            {
                if (_CurrentInverterExecution != value)
                {
                    _CurrentInverterExecution = value;
                    this.RaisePropertyChanged("CurrentInverterExecution");

                }
            }
        }

        private CellularAutomata _CurrentCellularAutomata;
        /// <summary>
        /// Gets or sets the current cellular automata.
        /// </summary>
        /// <value>
        /// The current cellular automata.
        /// </value>
        public CellularAutomata CurrentCellularAutomata
        {
            get
            {
                return _CurrentCellularAutomata;
            }
            set
            {
                if (_CurrentCellularAutomata != value)
                {
                    _CurrentCellularAutomata = value;
                    this.RaisePropertyChanged("CurrentCellularAutomata");

                }
            }
        }
        
        private EnumInverterParametersEditMode _EditMode;
        /// <summary>
        /// Gets or sets the edit mode.
        /// </summary>
        /// <value>
        /// The edit mode.
        /// </value>
        public EnumInverterParametersEditMode EditMode
        {
            get
            {
                return _EditMode;
            }
            set
            {
                if (_EditMode != value)
                {
                    _EditMode = value;
                    this.RaisePropertyChanged("EditMode");
                    OnEditModeChanged();
                }
            }
        }

        /// <summary>
        /// Called when [edit mode changed].
        /// </summary>
        private void OnEditModeChanged()
        {
            this.IsNeighbourhoodTypeEnabled = (this.EditMode == EnumInverterParametersEditMode.New);
        }


        private bool _IsNeighbourhoodTypeEnabled = true;
        /// <summary>
        /// Gets or sets the is neighbourhood type enabled.
        /// </summary>
        /// <value>
        /// The is neighbourhood type enabled.
        /// </value>
        public bool IsNeighbourhoodTypeEnabled
        {
            get
            {
                return _IsNeighbourhoodTypeEnabled;
            }
            set
            {
                if (_IsNeighbourhoodTypeEnabled != value)
                {
                    _IsNeighbourhoodTypeEnabled = value;
                    this.RaisePropertyChanged("IsNeighbourhoodTypeEnabled");

                }
            }
        }

               

        /// <summary>
        /// Handles the Click event of the OKButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void ButtonValidateAndAcceptButton_Click(object sender, RoutedEventArgs e)
        {
            if (this.CurrentCellularAutomata.NeighbourhoodTypeId == 0)
            {
                MessageBox.Show("Please choose the neibourhood type first", "Validation Error", MessageBoxButton.OK);
                return;
            }

            if (string.IsNullOrEmpty(this.CurrentCellularAutomata.Name))
            {
                MessageBox.Show("Please choose a name", "Validation Error", MessageBoxButton.OK);
                return;
            }

            if (string.IsNullOrEmpty(this.CurrentCellularAutomata.Description))
            {
                MessageBox.Show("Please choose a description", "Validation Error", MessageBoxButton.OK);
                return;
            }

           //create the cellular hierarchy data
            if (this.EditMode == EnumInverterParametersEditMode.New)
            {
                if (this.CurrentCellularAutomata.ImmunityTime == 0)   //when there is no immunity, we automatically generate the contamination rules
                {
                    StringBuilder sb = new StringBuilder();
                    sb.Append("Contamination Rules were created by default. ");

                    //first clear all the existing contamination rules. starting from the end            
                    for (int i = this.CurrentCellularAutomata.TransitionRules.Count - 1; i >= 0; i--)
                    {
                        TransitionRule existingContaminationRule = this.CurrentCellularAutomata.TransitionRules.ElementAt(i);
                        InverterController.Instance.RemoveTransitionRule(existingContaminationRule);
                    }

                    EntityHelper.GenerateContaminationRule(this.CurrentCellularAutomata, comboBoxNeighbourhoodType.SelectedItem as NeighbourhoodType, ref numberOfRulesCreated);
                   
                    MessageBox.Show(sb.ToString(), "Information", MessageBoxButton.OK);
                }                    
            }
                        
            this.DialogResult = true;
        }

    

        /// <summary>
        /// Handles the Click event of the CancelButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }        

        /// <summary>
        /// Handles the Click event of the ButtonRemoveTransitionRule control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void ButtonRemoveTransitionRule_Click(object sender, RoutedEventArgs e)
        {
            Button buttonRemove = sender as Button;
            TransitionRule transitionRuleToDelete = buttonRemove.CommandParameter as TransitionRule;

            MessageBoxResult result = MessageBox.Show("Do you really want to remove the transition rule <" + transitionRuleToDelete.Name + ">?", "Delete Rule Confirmation", MessageBoxButton.OKCancel);
            if (result == MessageBoxResult.OK)
            {
                InverterController.Instance.RemoveTransitionRule(transitionRuleToDelete);
            }
        }

        private void stackOfRules_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ;
        }


        #region INotifiedPropertyChange
        public event PropertyChangedEventHandler PropertyChanged;
        /// <summary>
        /// Raises the property changed.
        /// </summary>
        /// <param name="propertyName">Name of the property.</param>
        protected virtual void RaisePropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        } 
        #endregion
        
    }
}

